import wepy from 'wepy'
export default class extends wepy.mixin {
  data = {
    showCart: false,
    cart: []
  }
  onLoad() {
    if (wepy.getStorageSync('cart')) {
      this.showCart = true
    }
    this.cart = this.$parent.globalData.cart || []
  }
  methods = {
    countChange(e) {
      console.log(e)
      this.$parent.changeCount(e.target.dataset.id, e.detail)
    },
    statusChange(e) {
      console.log(e)

      this.$parent.changeStatus(e.target.dataset.id, e.detail)
    },
    remove(id) {
      console.log(id)
      this.$parent.removeGoods(id)
    },
    allCheckChange(e) {
      this.$parent.changeAllCheck(e.detail)
    },
    order() {
      if (this.amount <= 0) {
        return wepy.baseToast('订单金额不能为空！')
      }
      wepy.navigateTo({
        url: '/pages/order'
      })
    }
  }
  computed = {
    amount() {
      let total = 0
      this.cart.forEach((element) => {
        if (element.isCheck) {
          total += element.price * element.count
        }
      })
      return total * 100
    },
    isAllCheck() {
      let c = 0
      this.cart.forEach((element) => {
        if (element.isCheck) {
          c++
        }
      })
      return this.cart.length === c
    }
  }
}
